package com.uatair.common.config;

import com.uatair.common.client.MqttPushClient;
import com.uatair.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author 14820
 */
@Component
@ConfigurationProperties("spring.mqtt")
public class MqttConfig {

    @Autowired
    private MqttPushClient mqttPushClient;

    /**
     * 第一个MQTT服务的配置
     */
    private MqttProperties primary;

    /**
     * 第二个MQTT服务的配置
     */
    private MqttProperties secondary;

    public static class MqttProperties {
        /**
         * 用户名
         */
        private String username;
        /**
         * 密码
         */
        private String password;
        /**
         * 连接地址
         */
        private String hostUrl;
        /**
         * 客户Id
         */
        private String clientId;
        /**
         * 默认连接话题
         */
        private String defaultTopic;
        /**
         * 超时时间
         */
        private int timeout;
        /**
         * 保持连接数
         */
        private int keepalive;
        /**
         * mqtt功能使能
         */
        private boolean enabled;

        // Getter和Setter方法
        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getHostUrl() {
            return hostUrl;
        }

        public void setHostUrl(String hostUrl) {
            this.hostUrl = hostUrl;
        }

        public String getClientId() {
            return clientId;
        }

        public void setClientId(String clientId) {
            this.clientId = clientId;
        }

        public String getDefaultTopic() {
            return defaultTopic;
        }

        public void setDefaultTopic(String defaultTopic) {
            this.defaultTopic = defaultTopic;
        }

        public int getTimeout() {
            return timeout;
        }

        public void setTimeout(int timeout) {
            this.timeout = timeout;
        }

        public int getKeepalive() {
            return keepalive;
        }

        public void setKeepalive(int keepalive) {
            this.keepalive = keepalive;
        }

        public boolean isEnabled() {
            return enabled;
        }

        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    }

    public MqttProperties getPrimary() {
        return primary;
    }

    public void setPrimary(MqttProperties primary) {
        this.primary = primary;
    }

    public MqttProperties getSecondary() {
        return secondary;
    }

    public void setSecondary(MqttProperties secondary) {
        this.secondary = secondary;
    }

    @Bean("primaryMqttClient")
    public MqttPushClient getPrimaryMqttPushClient() {
        if (primary != null && primary.isEnabled()) {
            String[] mqtt_topic = StringUtils.split(primary.getDefaultTopic(), ",");
            mqttPushClient.connect(primary.getHostUrl(), primary.getClientId() + get4Num(),
                    primary.getUsername(), primary.getPassword(),
                    primary.getTimeout(), primary.getKeepalive());//连接
            for (String s : mqtt_topic) {
                mqttPushClient.subscribe(s, 0);//订阅主题
            }
        }
        return mqttPushClient;
    }

    @Bean("secondaryMqttClient")
    public MqttPushClient getSecondaryMqttPushClient() {
        if (secondary != null && secondary.isEnabled()) {
            String[] mqtt_topic = StringUtils.split(secondary.getDefaultTopic(), ",");
            mqttPushClient.connect(secondary.getHostUrl(), secondary.getClientId() + get4Num(),
                    secondary.getUsername(), secondary.getPassword(),
                    secondary.getTimeout(), secondary.getKeepalive());//连接
            for (String s : mqtt_topic) {
                mqttPushClient.subscribe(s, 0);//订阅主题
            }
        }
        return mqttPushClient;
    }

    private static String get4Num() {
        Random random = new Random();
        int randomNumber = 1000 + random.nextInt(9000);
        return randomNumber+"";
    }
}

